package com.atguigu.gulimall.order.controller;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderReturnReasonEntity;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @Author: fd
 * @Date 2021/8/23 18:18
 * @Description: 测试mq  @RabbitHandler
 */
@RestController
public class RabbitController {

	final RabbitTemplate rabbitTemplate;

	public RabbitController(RabbitTemplate rabbitTemplate) {
		this.rabbitTemplate = rabbitTemplate;
	}

	@RequestMapping("/sendMq")
	public String sendMessageHandlerTest(@RequestParam(value = "num",defaultValue = "10") Integer num) {
		for(int i = 0; i < num; i++){
			if(i % 2 == 0){
				OrderReturnReasonEntity reasonEntity = new OrderReturnReasonEntity();
				reasonEntity.setId(Long.valueOf(String.valueOf(i)));
				reasonEntity.setName("测试RabbitHandler-偶数-"+i);
				reasonEntity.setStatus(1);
				reasonEntity.setSort(2);
				rabbitTemplate.convertAndSend("hello-java-exchange","hello.java",
						reasonEntity,new CorrelationData(UUID.randomUUID().toString()));
			}else{
				OrderEntity orderEntity = new OrderEntity();
				orderEntity.setId(Long.valueOf(String.valueOf(i)));
				orderEntity.setReceiverName("测试RabbitHandler-奇数-"+i);
				rabbitTemplate.convertAndSend("hello-java-exchange","hello2.java",
						orderEntity,new CorrelationData(UUID.randomUUID().toString()));
			}
		}
		return "ok";
	}

}
